<template>
  <div class="main">
    <div class="bmi-calculator">
      <div class="header">
        <h1>BMI 计算器</h1>
        <p>计算您的身体质量指数，了解健康状况</p>
      </div>

      <div class="main-content">
        <!-- 计算器卡片 -->
        <div class="calculator-card">
          <div class="input-section">
            <div class="input-group">
              <label for="height">身高</label>
              <div class="input-with-unit">
                <input
                  id="height"
                  type="number"
                  v-model.number="height"
                  placeholder="请输入身高"
                  min="50"
                  max="250"
                  step="0.1"
                />
                <span class="unit">厘米</span>
              </div>
              <div class="input-range">
                <input type="range" v-model.number="height" min="50" max="250" />
              </div>
            </div>

            <div class="input-group">
              <label for="weight">体重</label>
              <div class="input-with-unit">
                <input
                  id="weight"
                  type="number"
                  v-model.number="weight"
                  placeholder="请输入体重"
                  min="10"
                  max="200"
                  step="0.1"
                />
                <span class="unit">千克</span>
              </div>
              <div class="input-range">
                <input type="range" v-model.number="weight" min="10" max="200" />
              </div>
            </div>

            <div class="unit-toggle">
              <button
                class="toggle-btn"
                :class="{ active: unitSystem === 'metric' }"
                @click="unitSystem = 'metric'"
              >
                公制 (cm/kg)
              </button>
              <button
                class="toggle-btn"
                :class="{ active: unitSystem === 'imperial' }"
                @click="unitSystem = 'imperial'"
              >
                英制 (ft/lb)
              </button>
            </div>
          </div>

          <!-- 结果展示 -->
          <div class="result-section" v-if="bmi > 0">
            <div class="bmi-value">
              <h3>您的 BMI</h3>
              <div class="value">{{ bmi.toFixed(1) }}</div>
              <div class="category" :class="bmiCategory.class">
                {{ bmiCategory.name }}
              </div>
            </div>

            <div class="bmi-scale">
              <div class="scale-labels">
                <span>偏瘦</span>
                <span>健康</span>
                <span>超重</span>
                <span>肥胖</span>
              </div>
              <div class="scale-bar">
                <div class="scale-fill" :style="scaleFillStyle"></div>
                <div class="scale-indicator" :style="indicatorStyle"></div>
              </div>
              <div class="scale-markers">
                <span>18.5</span>
                <span>25</span>
                <span>30</span>
              </div>
            </div>
          </div>

          <!-- 健康建议 -->
          <div class="advice-section" v-if="bmi > 0">
            <h3>健康建议</h3>
            <p>{{ bmiCategory.advice }}</p>
            <div class="health-tips">
              <div class="tip" v-for="(tip, index) in healthTips" :key="index">
                <span class="tip-icon">💡</span>
                <span>{{ tip }}</span>
              </div>
            </div>
          </div>
        </div>

        <!-- BMI 信息 -->
        <div class="info-section">
          <h2>关于 BMI</h2>
          <div class="info-grid">
            <div class="info-card">
              <div class="info-icon">📊</div>
              <h3>什么是 BMI?</h3>
              <p>身体质量指数（BMI）是衡量体重与身高比例的常用指标，用于评估体重状况。</p>
            </div>
            <div class="info-card">
              <div class="info-icon">⚖️</div>
              <h3>如何计算?</h3>
              <p>
                BMI = 体重(kg) / 身高(m)²。例如：体重70kg，身高1.75m，BMI = 70 / (1.75×1.75) = 22.9
              </p>
            </div>
            <div class="info-card">
              <div class="info-icon">📝</div>
              <h3>注意事项</h3>
              <p>
                BMI只是参考指标，不能完全反映身体脂肪分布和肌肉比例。运动员和老年人可能需要额外评估。
              </p>
            </div>
          </div>

          <!-- BMI 分类表 -->
          <div class="bmi-table">
            <h3>BMI 分类标准</h3>
            <table>
              <thead>
                <tr>
                  <th>分类</th>
                  <th>BMI 范围</th>
                  <th>健康风险</th>
                </tr>
              </thead>
              <tbody>
                <tr
                  v-for="category in bmiCategories"
                  :key="category.name"
                  :class="{ active: bmi >= category.min && bmi < category.max }"
                >
                  <td>
                    <span class="category-dot" :class="category.class"></span>
                    {{ category.name }}
                  </td>
                  <td>{{ category.range }}</td>
                  <td>{{ category.risk }}</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>

      <div class="footer">
        <p>BMI 计算器 - 仅供健康参考，如有疑问请咨询专业医生</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'

// 响应式数据
const height = ref(170)
const weight = ref(65)
const unitSystem = ref('metric') // 'metric' 或 'imperial'

// BMI 分类标准
const bmiCategories = [
  {
    name: '严重偏瘦',
    min: 0,
    max: 16,
    class: 'underweight-severe',
    range: '< 16',
    risk: '高风险',
    advice: '您的体重严重不足，可能存在健康风险，建议咨询医生和营养师。'
  },
  {
    name: '中度偏瘦',
    min: 16,
    max: 17,
    class: 'underweight-moderate',
    range: '16 - 16.9',
    risk: '中高风险',
    advice: '您的体重偏轻，建议增加营养摄入，适当进行力量训练增加肌肉量。'
  },
  {
    name: '轻度偏瘦',
    min: 17,
    max: 18.5,
    class: 'underweight-mild',
    range: '17 - 18.4',
    risk: '低风险',
    advice: '您的体重略轻，建议均衡饮食，适当增加健康食物的摄入。'
  },
  {
    name: '正常范围',
    min: 18.5,
    max: 25,
    class: 'normal',
    range: '18.5 - 24.9',
    risk: '正常',
    advice: '恭喜！您的体重在健康范围内，继续保持均衡饮食和适量运动。'
  },
  {
    name: '超重',
    min: 25,
    max: 30,
    class: 'overweight',
    range: '25 - 29.9',
    risk: '低风险',
    advice: '您的体重略高，建议适当控制饮食，增加体育锻炼。'
  },
  {
    name: '一级肥胖',
    min: 30,
    max: 35,
    class: 'obese-1',
    range: '30 - 34.9',
    risk: '中风险',
    advice: '您已进入肥胖范围，建议制定减重计划，控制热量摄入并增加运动量。'
  },
  {
    name: '二级肥胖',
    min: 35,
    max: 40,
    class: 'obese-2',
    range: '35 - 39.9',
    risk: '高风险',
    advice: '您的肥胖程度较高，健康风险增加，建议寻求专业医疗帮助制定减重方案。'
  },
  {
    name: '三级肥胖',
    min: 40,
    max: 100,
    class: 'obese-3',
    range: '≥ 40',
    risk: '极高风险',
    advice: '您的肥胖程度严重，健康风险极高，请立即咨询医生制定专业治疗方案。'
  }
]

// 健康建议
const healthTips = [
  '保持均衡饮食，多吃蔬菜水果',
  '每周进行至少150分钟中等强度运动',
  '保证充足的睡眠和水分摄入',
  '减少加工食品和高糖饮料的摄入',
  '定期监测体重变化'
]

// 计算属性
const bmi = computed(() => {
  if (!height.value || !weight.value) return 0

  let heightInMeters
  let weightInKg

  if (unitSystem.value === 'metric') {
    // 公制单位：厘米转米，千克不变
    heightInMeters = height.value / 100
    weightInKg = weight.value
  } else {
    // 英制单位：英尺转米，磅转千克
    heightInMeters = height.value * 0.3048 // 1英尺 = 0.3048米
    weightInKg = weight.value * 0.453592 // 1磅 = 0.453592千克
  }

  const bmiValue = weightInKg / (heightInMeters * heightInMeters)
  return bmiValue > 0 ? bmiValue : 0
})

// 获取BMI分类
const bmiCategory = computed(() => {
  return bmiCategories.find(category =>
    bmi.value >= category.min && bmi.value < category.max
  ) || bmiCategories[3] // 默认为正常范围
})

// 计算尺度填充样式
const scaleFillStyle = computed(() => {
  const percentage = Math.min((bmi.value / 40) * 100, 100)
  return {
    width: `${percentage}%`
  }
})

// 计算指示器位置
const indicatorStyle = computed(() => {
  const percentage = Math.min((bmi.value / 40) * 100, 100)
  return {
    left: `${percentage}%`
  }
})

// 监听单位系统变化，转换数值
watch(unitSystem, (newSystem, oldSystem) => {
  if (newSystem === 'metric' && oldSystem === 'imperial') {
    // 英制转公制
    height.value = Math.round(height.value * 30.48) // 英尺转厘米
    weight.value = Math.round(weight.value * 0.453592 * 10) / 10 // 磅转千克
  } else if (newSystem === 'imperial' && oldSystem === 'metric') {
    // 公制转英制
    height.value = Math.round((height.value / 30.48) * 10) / 10 // 厘米转英尺
    weight.value = Math.round((weight.value / 0.453592) * 10) / 10 // 千克转磅
  }
})
</script>

<style scoped>
.main {
  width: 100%;
  min-height: 100vh;
  background: linear-gradient(90deg, #a3e4a6 0%, #83b685 100%);
}
.bmi-calculator {
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.header {
  background: linear-gradient(90deg, #4caf50 0%, #45a049 100%);
  color: white;
  padding: 20px;
  text-align: center;
}

.header h1 {
  font-size: 28px;
  margin-bottom: 10px;
}

.header p {
  opacity: 0.9;
}

.main-content {
  padding: 20px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 30px;
}

.calculator-card {
  background: #f8f9fa;
  border-radius: 12px;
  padding: 25px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.input-section {
  margin-bottom: 25px;
}

.input-group {
  margin-bottom: 20px;
}

.input-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #34495e;
}

.input-with-unit {
  position: relative;
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.input-with-unit input {
  flex: 1;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 16px;
  background: white;
}

.unit {
  position: absolute;
  right: 15px;
  color: #7f8c8d;
  font-size: 14px;
}

.input-range input {
  width: 100%;
  margin-top: 5px;
}

.unit-toggle {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.toggle-btn {
  flex: 1;
  padding: 10px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s;
}

.toggle-btn.active {
  background: #4caf50;
  color: white;
  border-color: #4caf50;
}

.result-section {
  margin: 25px 0;
  padding: 20px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.bmi-value {
  text-align: center;
  margin-bottom: 25px;
}

.bmi-value h3 {
  margin: 0 0 10px;
  color: #7f8c8d;
  font-size: 16px;
}

.bmi-value .value {
  font-size: 48px;
  font-weight: bold;
  color: #2c3e50;
  margin-bottom: 10px;
}

.category {
  display: inline-block;
  padding: 6px 15px;
  border-radius: 20px;
  font-weight: 500;
  font-size: 14px;
}

.bmi-scale {
  margin-top: 20px;
}

.scale-labels {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 12px;
  color: #7f8c8d;
}

.scale-bar {
  position: relative;
  height: 12px;
  background: #e0e0e0;
  border-radius: 6px;
  overflow: hidden;
}

.scale-fill {
  height: 100%;
  background: linear-gradient(90deg, #4caf50, #ffc107, #ff9800, #f44336);
  border-radius: 6px;
  transition: width 0.5s ease;
}

.scale-indicator {
  position: absolute;
  top: -4px;
  width: 4px;
  height: 20px;
  background: #2c3e50;
  border-radius: 2px;
  transform: translateX(-50%);
  transition: left 0.5s ease;
}

.scale-markers {
  display: flex;
  justify-content: space-between;
  margin-top: 5px;
  font-size: 11px;
  color: #7f8c8d;
}

.advice-section {
  margin-top: 25px;
  padding: 20px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.advice-section h3 {
  margin: 0 0 15px;
  color: #2c3e50;
}

.health-tips {
  margin-top: 15px;
}

.tip {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  padding: 8px 0;
}

.tip-icon {
  font-size: 16px;
}

.info-section {
  padding: 10px 0;
}

.info-section h2 {
  margin-bottom: 20px;
  color: #2c3e50;
  border-bottom: 2px solid #eee;
  padding-bottom: 10px;
}

.info-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  margin-bottom: 30px;
}

.info-card {
  padding: 20px;
  background: #f8f9fa;
  border-radius: 10px;
  border-left: 4px solid #4caf50;
}

.info-icon {
  font-size: 32px;
  margin-bottom: 15px;
}

.info-card h3 {
  margin: 0 0 10px;
  color: #2c3e50;
}

.info-card p {
  margin: 0;
  color: #7f8c8d;
  line-height: 1.5;
}

.bmi-table {
  margin-top: 30px;
}

.bmi-table h3 {
  margin-bottom: 15px;
  color: #2c3e50;
}

table {
  width: 100%;
  border-collapse: collapse;
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

th {
  background: #4caf50;
  color: white;
  padding: 12px 15px;
  text-align: left;
  font-weight: 500;
}

td {
  padding: 12px 15px;
  border-bottom: 1px solid #eee;
}

tr:last-child td {
  border-bottom: none;
}

tr.active {
  background: #f0f9f0;
}

.category-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 8px;
}

.underweight-severe {
  background: #bbdefb;
}
.underweight-moderate {
  background: #90caf9;
}
.underweight-mild {
  background: #64b5f6;
}
.normal {
  background: #4caf50;
}
.overweight {
  background: #ffc107;
}
.obese-1 {
  background: #ff9800;
}
.obese-2 {
  background: #ff5722;
}
.obese-3 {
  background: #f44336;
}

.footer {
  background: #f8f9fa;
  padding: 15px;
  text-align: center;
  color: #7f8c8d;
  font-size: 14px;
}

@media (max-width: 768px) {
  .main-content {
    grid-template-columns: 1fr;
    gap: 20px;
  }

  .info-grid {
    grid-template-columns: 1fr;
  }

  table {
    font-size: 14px;
  }

  th,
  td {
    padding: 8px 10px;
  }
}
</style>